//
//  FLLGoodAndBadViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/13.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLGoodAndBadViewController.h"
#import "FLLMacro.h"
#import "FLLGoodAndBadModel.h"
#import "FLLFoodMaterialTableViewCell.h"
#import "FLLGoodAndBadTableViewCell.h"
#import "Masonry.h"
#import "Good_Suitable_Not_With.h"
#import "GoodAndBadData.h"
#import "GoodAndBadMaterial.h"
#import "GoodAndBadSuitable_With.h"

@interface FLLGoodAndBadViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *goodAndBadTableView;
@property (nonatomic, strong) FLLGoodAndBadModel *goodAndBadModel;
@property (nonatomic, strong) NSArray<GoodAndBadSuitable_With *> *goodArray;
@property (nonatomic, strong) NSArray<Good_Suitable_Not_With *> *badArray;
@end

@implementation FLLGoodAndBadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createPOSTRequest];
    [self createGoodAndBadTableView];
   

   
}

- (void)createPOSTRequest
{
    
    
    NSString *bodyString = [NSString stringWithFormat:@"methodName=DishesSuitable&dishes_id=%@&version=4.40&", _idString];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.goodAndBadModel = [FLLGoodAndBadModel modelWithDic:result];
        self.goodArray = self.goodAndBadModel.data.material.suitable_with;
        self.badArray = self.goodAndBadModel.data.material.suitable_not_with;
        [self.goodAndBadTableView reloadData];
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)createGoodAndBadTableView
{

    self.goodAndBadTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 95 * kHeightScale) style:UITableViewStylePlain];
    self.goodAndBadTableView.delegate = self;
    self.goodAndBadTableView.dataSource = self;
    self.goodAndBadTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.goodAndBadTableView.bounces = NO;
    [self.goodAndBadTableView registerNib:[UINib nibWithNibName:@"FLLFoodMaterialTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodMaterialTableCell"];
    [self.goodAndBadTableView registerNib:[UINib nibWithNibName:@"FLLGoodAndBadTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"goodAndBadTableCell"];
    [self.view addSubview:_goodAndBadTableView];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 0) {
        return 1;
    } if (section == 1) {
        return _goodArray.count;
    }
    return _badArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
    
        FLLFoodMaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodMaterialTableCell"];
        [cell.materialImage sd_setImageWithURL:[NSURL URLWithString:self.goodAndBadModel.data.material.image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.blackImage.image = [UIImage new];
        return cell;
    }
    FLLGoodAndBadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodAndBadTableCell"];
    cell.titleLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    cell.contentLabel.font = [UIFont systemFontOfSize:13 *kWidthScale];

    if (indexPath.section == 1) {
        [cell.dishImage sd_setImageWithURL:[NSURL URLWithString:_goodArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.titleLabel.text = _goodArray[indexPath.row].material_name;
        cell.contentLabel.text = _goodArray[indexPath.row].suitable_desc;

    } if (indexPath.section == 2) {
    
        [cell.dishImage sd_setImageWithURL:[NSURL URLWithString:_badArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.titleLabel.text = _badArray[indexPath.row].material_name;
        cell.contentLabel.text = _badArray[indexPath.row].suitable_desc;
       
    }
    return cell;
   
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        return SCREEN_WIDTH * 2 / 3.0;
    }
    return 60;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UILabel *grayLabel = [[UILabel alloc] init];
    [sectionHeaderView addSubview:grayLabel];
    grayLabel.backgroundColor = FLLLightGrayColor;
    [grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(10);
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    NSString *sectionLabel = self.goodAndBadModel.data.material.material_name;
    
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont systemFontOfSize:16 * kWidthScale];
    if (section == 1) {
        NSString *sectionTitle = [NSString stringWithFormat:@"与%@搭配相宜的食材", sectionLabel];
        /** 关键字高亮*/
        NSString *highLightString = @"相宜";
        NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:sectionTitle];
        NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16 * kWidthScale], NSForegroundColorAttributeName : [UIColor greenColor]};
        NSRange range = [sectionTitle rangeOfString:highLightString];
        [attribut addAttributes:dic range:range];
        titleLabel.attributedText = attribut;

    } if (section == 2) {
        NSString *sectionTitle = [NSString stringWithFormat:@"与%@搭配相克的食材", sectionLabel];
        /** 关键字高亮*/
        NSString *highLightString = @"相克";
        NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:sectionTitle];
        NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16 * kWidthScale], NSForegroundColorAttributeName : [UIColor redColor]};
        NSRange range = [sectionTitle rangeOfString:highLightString];
        [attribut addAttributes:dic range:range];
        titleLabel.attributedText = attribut;
    }
    [sectionHeaderView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(grayLabel.mas_bottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    return sectionHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    if (section == 0) {
        return 0;
    }
    return 40;
}
- (void)setIdString:(NSString *)idString
{
    _idString = idString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 在这里不需要取消去区头悬浮
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//
//
//    NSLog(@"%lf", scrollView.contentOffset.y);
//    CGFloat sectionHeaderHeight = 40; //区头高度
//    if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y >=0) {
//        scrollView.contentInset = UIEdgeInsetsMake(- scrollView.contentOffset.y, 0, 0, 0);
//    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
//        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//    }
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
